package o2o.task.user;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.client.distribute.UserDistributeClient;
import com.jinyouapp.model.distribute.UserDistributeInfo;
import o2o.dao.PO.PojoTables;
import org.apache.commons.lang.StringUtils;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.system.data.SYS_SETTINGS;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserDistributeInfoSyncUtils {

    public static void threadDeal() {
        // 查询最近发生变化的用户
        String sql = "select *,signPhoto as iconUrl from " + PojoTables.UserInfo + " where updateTime>='" + DateUtils.sdfDateTime.get().format(new Date().getTime() - 5 * 60 * 1000L) + "'";
        List<UserDistributeInfo> list = null;
        try {
            list = (List<UserDistributeInfo>) UserDaoSql.getInstance().list(UserDistributeInfo.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isNotEmpty(list.get(i).getIconUrl()) && ValidateUtil.isNotNetUrl(list.get(i).getIconUrl())) {
                    list.get(i).setIconUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getIconUrl());
                }
            }
        }
        if (list != null && !list.isEmpty()) {
            deal(list);
        }
    }

    public static void syncByUsername(String username) {
        if (ValidateUtil.isNull(username)) {
            return;
        }
        if (null != SYS_SETTINGS.IS_OPEN_DISTRIBUTION && 0 == SYS_SETTINGS.IS_OPEN_DISTRIBUTION) {//未开启分销的话
            return;
        }
        // 查询最近发生变化的用户
        String sql = "select *,signPhoto as iconUrl from " + PojoTables.UserInfo + " where delFlag =0 and username='" + username + "'";
        List<UserDistributeInfo> list = null;
        try {
            list = (List<UserDistributeInfo>) UserDaoSql.getInstance().list(UserDistributeInfo.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isNotEmpty(list.get(i).getIconUrl()) && ValidateUtil.isNotNetUrl(list.get(i).getIconUrl())) {
                    list.get(i).setIconUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + list.get(i).getIconUrl());
                }
            }
        }
        if (list != null && !list.isEmpty()) {
            deal(list);
        }
    }

    public static void deal(List<UserDistributeInfo> list) {
        new UserDistributeClient().syncDistributeUserInfo(SYS_CONFIG_PATH.FEN_XIAO_SERVICE_KEY, list);
    }

    // 全部更新
    public static void deal() {
        String sql = "select * from " + PojoTables.UserInfo + " where delFlag = 0";
        List<UserDistributeInfo> list = null;
        try {
            list = (List<UserDistributeInfo>) UserDaoSql.getInstance().list(UserDistributeInfo.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null && !list.isEmpty()) {
            deal(list);
        }
    }
}
